<template>
  <div id="app">
    <!-- <transition :name="transitionName"> -->
      <keep-alive :include="cache">
        <router-view></router-view>
      </keep-alive>
    <!-- </transition> -->
  </div>
</template>

<script>
import { mobileLogin } from "@/api";
export default {
  name: "app",
  data() {
    return {
      transitionName: "van-slide-right", //切换动画
      cache: ['index'] //可以结合vuex按需缓存
    };
  },
  watch: {
    $route(to, from) {
      let isBack = this.$router.isBack; // 监听路由变化时的状态为前进还是后退
      if (isBack) {
        this.transitionName = "van-slide-left";
      } else {
        this.transitionName = "van-slide-right";
      }
      this.$router.isBack = false;
    }
  },
  created() {
    // this.wxLogin()
  },
  methods: {
    wxLogin() {
      let code = this.getQueryString("code");
      if (code) {
        mobileLogin({
          code: code
        }).then(res => {
          if (res.code == 200) {
            sessionStorage.setItem(
              "userInfo",
              JSON.stringify(res.data.userInfo)
            );
          } else {
            this.refresh();
          }
        });
      } else {
        this.refresh();
      }
    },
    getQueryString(name) {
      let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      let r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return null;
    },
    refresh() {
      let address = window.location.href.split("#/")[1];
      let url = encodeURIComponent(
        "http://pergo.weilang.top/pergo-mobile/#/" + address
      );
      window.location.href =
        "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx73a44bbd78e9cf99&redirect_uri=" +
        url +
        "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
    }
  }
};
</script>

<style lang="less">
@import "~vux/src/styles/reset.less";
@import "./assets/css/common.css";
</style>
